# -*- coding: utf-8 -*-            
# @Author : HJH
# @Time : 2024/7/29 13:02
# @Use :
import time
from urllib.parse import quote_plus

import pandas as pd
import requests
import json

from pymongo import MongoClient

ip = "222.89.84.32"
db_name = "py_policy"
port = "27027"
name = "admin"
pwd = "mongo@yc66.com"
# 连接到MongoDB
client = MongoClient(
    f'mongodb://{quote_plus(name)}:{quote_plus(pwd)}@222.89.84.32:27027/?authMechanism=DEFAULT&authSource=admin')

# 选择数据库
db = client['py_policy']

# 选择集合
collection = db['cz_b2b_data']


def filter_price(r_data):
    for data in r_data.get('data'):
        flight_price = data[0]

        for price in flight_price['cabinVoList']:
            if price['cabin'] not in ['I', 'D', 'C', 'B', 'M', 'H', 'J', 'U', 'A', 'L', 'E', 'V', 'Z', 'T', 'N'] or \
                    price['enjoyconInfoList'] is None:
                continue

            lc_eis = [en for en in price['enjoyconInfoList'] if en['productName'] == '里程/优惠退票']
            if len(lc_eis) == 0:
                continue

            lc_ei = lc_eis[0]
            new_data = dict(flight_price)
            new_data['cabinVoList'] = None
            new_data['cabin'] = price['cabin']
            new_data['discountPrice'] = price['discountPrice']
            new_data['productName'] = lc_ei['productName']
            new_data['priceAdult'] = lc_ei['priceAdult']
            new_data['enjoyconInfoList'] = None
            new_data['xRuleInfoGroupByActivityListAll'] = None
            new_data['xRuleInfoListPriorityAll'] = None
            new_data['tax'] = None
            collection.insert_one(new_data)
            res_list.append(new_data)


if __name__ == '__main__':
    # air_jobs = json.loads(airline)

    res_list = []
    for job in air_jobs:
        res_data = get_flight_list(job)
        if res_data is None or res_data.get('data', None) is None:
            print(f"{job['dep']}->{job['arr']} {job['dep_date']} is {json.dumps(res_data)}")
            continue
        else:
            print(f"{job['dep']}->{job['arr']} {job['dep_date']} is ok... ...")
        filter_price(res_data)

    df = pd.DataFrame(res_list)

    # 保存合并后的数据到Excel文件
    df.to_excel('南航B2B数据.xlsx', index=False)
